package br.com.naybee.client.views.add.configuracao;

import br.com.naybee.client.NaybeeService;
import br.com.naybee.client.views.add.menu.configuracao.MenuConfiguracaoView;
import br.com.naybee.client.views.base.Principal;
import br.com.naybee.framework.client.componente.gwt.event.CommEvent;
import br.com.naybee.framework.client.componente.gwt.event.CommHandler;
import br.com.naybee.framework.client.componente.gwt.web.Div;
import br.com.naybee.framework.client.componente.gwt.web.InputText;
import br.com.naybee.framework.client.componente.gwt.web.Option;
import br.com.naybee.framework.client.componente.gwt.web.SelectMultiple;
import br.com.naybee.framework.client.models.ListModel;
import br.com.naybee.framework.client.models.Model;
import br.com.naybee.framework.client.models.Models;
import br.com.naybee.framework.client.utils.NAYException;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class CadastrarRepartimentoView extends Composite {

	private static CadastrarRepartimentoViewUiBinder uiBinder = GWT.create(CadastrarRepartimentoViewUiBinder.class);

	interface CadastrarRepartimentoViewUiBinder extends UiBinder<Widget, CadastrarRepartimentoView> {
	}

	@UiField
	protected Div divGrupoRepartimento;
	@UiField
	protected Div divIcone;
	@UiField
	protected InputText descricao;

	private SelectMultiple selectMultipleGrupoRepartimento = new SelectMultiple();
	private SelectMultiple selectMultipleIcones = new SelectMultiple();

	public CadastrarRepartimentoView() {
		initWidget(uiBinder.createAndBindUi(this));
		init();
	}

	private void init() {
		selectMultipleGrupoRepartimento.setStyleName("select-div");
		selectMultipleIcones.setStyleName("select-div");
		initGrupoRepartimento();
		initIcones();
	}

	private void initGrupoRepartimento() {

		NaybeeService naybeeService = new NaybeeService("grupoRepartimentoFs", "listarRepartimentos");
		naybeeService.post(null, new CommHandler() {
			public void onSuccess(CommEvent event) {
				carregarGrupoRepartimento(event.getListModel());
			}

			public void onException(NAYException error) {
			}
		});
	}

	private void initIcones() {
		NaybeeService naybeeService = new NaybeeService("iconeFs", "listarIcones");
		naybeeService.post(null, new CommHandler() {
			public void onSuccess(CommEvent event) {
				carregarIcones(event.getListModel());
			}

			public void onException(NAYException error) {
			}
		});
	}

	private void carregarGrupoRepartimento(ListModel listModel) {
		for (Model model : listModel) {
			Option option = new Option();
			option.setClassId(model.getStr("idGrupoRepartimento"));
			option.setText(model.getStr("descricaoRepartimento").toLowerCase());
			selectMultipleGrupoRepartimento.add(option);
			divGrupoRepartimento.add(selectMultipleGrupoRepartimento);
		}
	}

	private void carregarIcones(ListModel listModel) {
		for (Model model : listModel) {
			Option option = new Option();
			option.setClassId(model.getStr("idIcone"));
			option.setText(model.getStr("icone"));
			selectMultipleIcones.add(option);
			divIcone.add(selectMultipleIcones);
		}
	}

	@UiHandler("btnSalvar")
	public void onClickBtnSalvar(ClickEvent event) {
		String idGrupoRepartimento = selectMultipleGrupoRepartimento.getOption().getId();
		String idIcones = selectMultipleIcones.getOption().getId();
		if (validarCampos()) {
			Model model = Models.newModel();
			model.add("descricaoRepartimento", descricao.getValue());
			model.add("idGrupoRepartimento", idGrupoRepartimento);
			model.add("idIcones", idIcones);
			salvar(model);
		} else {
			Window.alert("Preecha todos os campos");
		}
	}

	private void salvar(Model model) {
		NaybeeService naybeeService = new NaybeeService("repartimentoFs", "salvarRepartimento");
		naybeeService.post(model, new CommHandler() {
			public void onSuccess(CommEvent event) {
				Principal.get().add(new MenuConfiguracaoView());
			}

			public void onException(NAYException error) {
			}
		});
	}

	private boolean validarCampos() {
		if (descricao.getValue().equals("")) {
			return false;
		} else if (!selectMultipleGrupoRepartimento.isSelect()) {
			return false;
		} else if (!selectMultipleIcones.isSelect()) {
			return false;
		}
		return true;
	}

}
